package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.paas.redis.PaasRedisHelper;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.dingtalk.pojo.dto.BundleRobotManageEnableDTO;
import com.xbongbong.pro.dingtalk.pojo.dto.BundleRobotManageListDTO;
import com.xbongbong.pro.dingtalk.pojo.dto.BundleRobotManageSetDTO;
import com.xbongbong.pro.dingtalk.pojo.vo.BundleRobotManageEnableVO;
import com.xbongbong.pro.dingtalk.pojo.vo.BundleRobotManageListVO;
import com.xbongbong.pro.dingtalk.pojo.vo.BundleRobotManageSetVO;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.saas.service.DingRobotManageService;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * Created with IntelliJ IDEA
 *
 * @Auther: 宋港迎
 * @Date: 2020/09/16/14:18
 * @Destription:
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/bundle/robot/manage")
public class DingRobotManageController {

    @Resource
    private DingRobotManageService dingRobotManageService;
    @Resource
    private PaasRedisHelper paasRedisHelper;

    /**
     * 群机器人开启/关闭
     * @param bundleRobotManageEnableDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/enable", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String enable(@RequestBody @Valid BundleRobotManageEnableDTO bundleRobotManageEnableDTO, BindingResult br) {
        XbbResponse<BundleRobotManageEnableVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            // ----------------------------------repeat post----------------------------------------------
            // 判断是否跳过重复提交校验
            if (!Boolean.TRUE.equals(bundleRobotManageEnableDTO.getIgnoreConCheck())) {
                //重复提交校验
                if (!paasRedisHelper.checkConcurrentLock(bundleRobotManageEnableDTO)) {
                    response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                    return JSON.toJSONString(response);
                }
            }
            // ----------------------------------repeat post----------------------------------------------
            try {
                BundleRobotManageEnableVO bundleRobotManageEnableVO = dingRobotManageService.enable(bundleRobotManageEnableDTO);
                response = new XbbResponse<>(bundleRobotManageEnableVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 获取群机器人设置
     * @param bundleRobotManageListDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST , produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String list(@RequestBody @Valid BundleRobotManageListDTO bundleRobotManageListDTO, BindingResult br) {
        XbbResponse<BundleRobotManageListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                BundleRobotManageListVO bundleRobotManageListVO = dingRobotManageService.list(bundleRobotManageListDTO);
                response = new XbbResponse<>(bundleRobotManageListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 群机器人中的设置功能
     * @param bundleRobotManageSetDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/set", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String set(@RequestBody @Valid BundleRobotManageSetDTO bundleRobotManageSetDTO , BindingResult br) {
        XbbResponse<BundleRobotManageSetVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            // ----------------------------------repeat post----------------------------------------------
            // 判断是否跳过重复提交校验
            if (!Boolean.TRUE.equals(bundleRobotManageSetDTO.getIgnoreConCheck())) {
                //重复提交校验
                if (!paasRedisHelper.checkConcurrentLock(bundleRobotManageSetDTO)) {
                    response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                    return JSON.toJSONString(response);
                }
            }
            // ----------------------------------repeat post----------------------------------------------
            try {
                BundleRobotManageSetVO bundleRobotManageSetVO = dingRobotManageService.set(bundleRobotManageSetDTO);
                response = new XbbResponse<>(bundleRobotManageSetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response,SerializerFeature.DisableCircularReferenceDetect);
    }
}
